package sc.love.boot.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import sc.love.boot.kafka.KafkaProducer;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * springboot自动加载
 */
@Component
public class FileRead implements CommandLineRunner {

    @Autowired
    private KafkaProducer kafkaProducer;

    public  static  final  Map<String, String> map = new HashMap<>();
    /**
     * 将文件里的键值对存储到map中
     *
     * @param fileName
     * @return
     */
    public  Map<String, String> fileToList(String fileName) {

        try {
            //classpath路径
            File file = ResourceUtils.getFile(fileName);
//            File file = new File(fileName);  全路径
            if (file.exists()) {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String len;
                if ((len = bufferedReader.readLine()) != null) {
                    String[] split = len.split(":");
                    map.put(split[0], split[1]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    @Override
    public void run(String... args) throws Exception {
   //    kafkaProducer.send("jenkis---项目重新发布启动", "jenkis");

        String property = System.getProperty("user.dir");

        //非jar包写法
        fileToList("classpath:uspa.txt");
        //jar包写法 在Linux中无法直接访问未经解压的文件，所以就会找不到文件。
       // fileToList("/uspa.txt");
        runnerLog();
    }

    public void runnerLog() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formatDate = sdf.format(date);
        String userDir = System.getProperty("user.dir");//获取当前路径

        String directory = "/runnerLog";
        File file = new File(userDir+directory+"/runnerlog_" + formatDate);

            try {
                File directoryFile = new File(userDir + directory);
                if (!directoryFile.isDirectory()) {
                    boolean mkdir = directoryFile.mkdir();
                    System.out.println("创建目录成功："+mkdir);
                }
                if (!file.exists()) {
                    boolean newFile = file.createNewFile();
                    System.out.println("创建新文件成功："+newFile);
                }
                writeLog(file);
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    public void writeLog(File file) {
        FileWriter printWriter = null;
        try {
            printWriter = new FileWriter(file,true);
            printWriter.write("\n"+new Date()+"-启动" );
            printWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                printWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}